package com.gc.web.controller.api;

import com.gc.business.domain.FeedbackMessage;
import com.gc.business.service.IFeedbackMessageService;
import com.gc.common.constant.Constants;
import com.gc.common.utils.StringUtils;
import com.gc.framework.web.base.ApiBaseController;
import com.gc.system.domain.SysUser;
import com.gc.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.Map;

/**
 * @title 意见反馈api接口层
 * @Date 2020/12/03 14:27
 * @author hruiyan
 */
@CrossOrigin
@Controller
@RequestMapping("/api/feedback")
public class ApiFeedbackMessageController extends ApiBaseController {

    @Autowired
    private IFeedbackMessageService feedbackMessageService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 意见反馈提交接口
     * @param token
     * @param feedbackMessage
     * @return
     */
    @PostMapping("/submitFeedbackMessage")
    @ResponseBody
    public Map<String,Object> submitFeedbackMessage(String token, FeedbackMessage feedbackMessage){
        //获取提交人ID
        Long userId = getUserId(token);
        //获取提交人用户对象
        SysUser user = sysUserService.selectUserById(userId);

        //校验方面
        if(StringUtils.isEmpty(feedbackMessage.getFeedbackContext())){
            return error(Constants.UNVERIFY,"请上传意见反馈信息！@");
        }
        try {
            feedbackMessage.setCreateBy(user.getUserName());
            feedbackMessage.setCreateTime(new Date());
            feedbackMessage.setCompanyId(user.getAttrId());
            feedbackMessage.setSubmitterId(userId);
            feedbackMessageService.save(feedbackMessage);
        }catch (Exception e){
            e.printStackTrace();
            return error(Constants.UNVERIFY,"稍等一下，服务器开小差了！@");
        }
        return success(feedbackMessage);
    }






}
